﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Windows.Documents;
using WordSpider.Entities;

namespace WordSpider
{
    /// <summary>
    /// 单词管理
    /// </summary>
    public class WordManager
    {
        /// <summary>
        /// 获得所有图书
        /// </summary>
        public static List<BookEntity> GetAllBook()
        {
            return new List<BookEntity>()
                       {
                           new BookEntity(){BookId = 1, BookName = "中考词汇", FileName = "JuniorMiddle.txt", IsInput = true},
                           new BookEntity(){BookId = 2, BookName = "高考词汇", FileName = "SeniorMiddle.txt", IsInput = true},
                           new BookEntity(){BookId = 4, BookName = "全国大学英语四级考试", FileName = "cet4.txt", IsInput = true},
                           new BookEntity(){BookId = 6, BookName = "全国大学英语六级考试", FileName = "cet6.txt", IsInput = true},
                           new BookEntity(){BookId = 7, BookName = "研究生入学考试", FileName = "kaoyan.txt", IsInput = true},
                           new BookEntity(){BookId = 11, BookName = "新概念英语1", FileName = "nc1.txt", IsInput = true},
                           new BookEntity(){BookId = 12, BookName = "新概念英语2", FileName = "nc2.txt", IsInput = true},
                           new BookEntity(){BookId = 13, BookName = "新概念英语3", FileName = "nc3.txt", IsInput = true},
                           new BookEntity(){BookId = 14, BookName = "新概念英语4", FileName = "nc4.txt", IsInput = true},

                           new BookEntity(){BookId = 21, BookName = "TOFEL", FileName = "toefl.txt", IsInput = true},
                           new BookEntity(){BookId = 22, BookName = "IELTS", FileName = "ielts.txt", IsInput = true},
                           new BookEntity(){BookId = 23, BookName = "GRE", FileName = "gre.txt", IsInput = true},
                           //new BookEntity(){BookId = 14, BookName = "GMAT", FileName = "test.txt", IsInput = true},
                           //new BookEntity(){BookId = 21, BookName = "CET-4 Test", FileName = "test.txt", IsInput = true},
                           //new BookEntity(){BookId = 22, BookName = "CET-6 Test", FileName = "test2.txt", IsInput = true},
                       };
        }

        /// <summary>
        /// 指定单词是否存在
        /// </summary>
        /// <param name="word"></param>
        /// <returns></returns>
        public static bool IsWordExist(string word)
        {
            return new WordSpiderDBEntities().Table_English.Count(x => x.English == word) != 0;
        }

        /// <summary>
        /// 添加单词
        /// </summary>
        /// <param name="wordEntity"></param>
        /// <returns></returns>
        public static WordEntity AddWord(WordEntity wordEntity)
        {
            var db = new WordSpiderDBEntities();
            wordEntity.CreateDate = DateTime.Now;
            var tmp = new Table_English()
                          {
                              English = wordEntity.English,
                              CompleteData = wordEntity.ToXML(),
                              DownloadDate = wordEntity.CreateDate,
                          };
            db.AddToTable_English(tmp);
            db.SaveChanges();
            return wordEntity;
        }

        /// <summary>
        /// 生成单词记录
        /// </summary>
        /// <param name="english"></param>
        public static WordEntity GetWord(string english)
        {
            //单词是否存在
            if (IsWordExist(english))
            {
                return WordEntity.FromTable(new WordSpiderDBEntities().Table_English.First(x => x.English == english));
            }

            //下载&分析
            var word = WordDownloadManager.GetWordFromYouDao(english);

            //入库
            return WordManager.AddWord(word);
        }

        /// <summary>
        /// 执行词库升级动作
        /// </summary>
        public static void UpgradeWord(Action<int> totalAction, Action<int> indexAction)
        {
            var db = new WordSpiderDBEntities();

            totalAction(db.Table_English.Count());
            var index = 0;

            db.Table_English.ToList().ForEach(x =>
                                                  {
                                                      x.CompleteData =
           WordDownloadManager.GetWordFromYouDao(x.English).ToXML();
                                                      db.SaveChanges();
                                                      indexAction(++index);
                                                  });

        }

        /// <summary>
        /// 执行词库升级动作，下载Html代码
        /// </summary>
        public static void UpgradeWordForDownloadHtml(Action<int> totalAction, Action<int> indexAction)
        {
            var db = new WordSpiderDBEntities();

            totalAction(db.Table_English.Count());
            var index = 0;

            db.Table_English.ToList().ForEach(x =>
                {
                    x.SourceHtml =
                        WordDownloadManager.GetWordHtmlFromYouDao(x.English);
                    db.SaveChanges();
                    indexAction(++index);
                });
        }

        /// <summary>
        /// 下载单词的HTML文件
        /// </summary>
        /// <param name="englishs"></param>
        /// <param name="indexAction"></param>
        public static void DownloadHtmlAndSaveToDB(List<string> englishs, Action<int> indexAction)
        {
            var db = new WordSpiderDBEntities();

            var index = 0;

            englishs.ForEach(x =>
            {
                db.Table_English.AddObject(
                    new Table_English()
                        {
                            English = x,
                            SourceHtml = WordDownloadManager.GetWordHtmlFromYouDao(x),
                            DownloadDate = DateTime.Now,
                        });

                db.SaveChanges();
                indexAction(++index);
            });


        }


        /// <summary>
        /// 下载并保存从Bing下载的例句
        /// </summary>
        /// <param name="englishs"></param>
        /// <param name="indexAction"></param>
        public static void DownloadBingSentencesJsonSaveToDB(List<string> englishs, Action<int> indexAction)
        {
            var db = new WordSpiderDBEntities();

            var index = 0;

            englishs.ForEach(x =>
                                 {
                                     db.Table_English.First(y => y.English == x)
                                         .BingSentenceHtml = WordDownloadManager.GetSentenceJsonFromBing(x);
                                     db.SaveChanges();
                                     indexAction(++index);
                                     Thread.Sleep(100);
                                 });
        }


        /// <summary>
        /// 获得所有单词实体
        /// </summary>
        /// <returns></returns>
        public static List<WordEntity> GetAllWord()
        {
            var db = new WordSpiderDBEntities();
            var twords = db.Table_English.Select(x => new
                                                          {
                                                              English = x.English,
                                                              CompleteData = x.CompleteData,
                                                              DownloadDate = x.DownloadDate,
                                                          }).ToList().Select(x => new Table_English()
                                                                                      {
                                                                                          English = x.English,
                                                                                          CompleteData = x.CompleteData,
                                                                                          DownloadDate = x.DownloadDate,
                                                                                      }).ToList();
            var words = new List<WordEntity>();

            foreach (var tableEnglish in twords)
            {
                words.Add(WordEntity.FromTable(tableEnglish));
            }
            return words;
        }

        //public static void Save()
        //{
        //    var db = new WordSpiderDBEntities();
        //    foreach (var wordEntity in words)
        //    {
        //        db.AddToTable_BookEnglish(new Table_BookEnglish()
        //        {
        //            English = wordEntity.English,
        //            BookId = bookEntity.BookId,
        //        });
        //    }
        //}
    }

    /// <summary>
    /// 书籍实体类
    /// </summary>
    public class BookEntity
    {
        /// <summary>
        /// 书籍Id
        /// </summary>
        public int BookId { get; set; }

        /// <summary>
        /// 书籍名称
        /// </summary>
        public string BookName { get; set; }

        /// <summary>
        /// 导入文件名
        /// </summary>
        public string FileName { get; set; }

        /// <summary>
        /// 是否导入
        /// </summary>
        public bool IsInput { get; set; }
    }
}
